package com.gxt.maket.controller;

import com.gxt.maket.dto.ClueActivityDTO;
import com.gxt.maket.dto.ClueBusinessDTO;
import com.gxt.maket.service.IClueService;
import com.gxt.maket.domain.Clue;
import com.gxt.maket.query.ClueQuery;
import com.gxt.basic.utils.AjaxResult;
import com.gxt.basic.utils.PageList;
import com.gxt.maket.vo.ClueSourceStatisticalVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/clue")
public class ClueController {
    @Autowired
    public IClueService clueService;


    /**
     * 保存和修改公用的
     * @param clue  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult save(@RequestBody Clue clue){
        try {
            if( clue.getId()!=null)
                clueService.update(clue);
            else
                clueService.insert(clue);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            clueService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult loadById(@PathVariable("id")Long id)
    {
        try {
            Clue clue = clueService.loadById(id);
            return AjaxResult.me().setResult(clue);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult getAll(){

        try {
            List< Clue> list = clueService.loadAll();
            return AjaxResult.me().setResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult pageList(@RequestBody ClueQuery query)
    {
        try {
            PageList<Clue> pageList = clueService.pageList(query);
            return AjaxResult.me().setResult(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }

    @PostMapping("/activity")
    public AjaxResult clueBindActivity(@RequestBody ClueActivityDTO clueActivityDTO){
        try {
            clueService.clueBindActivity(clueActivityDTO);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }

    @PostMapping("/assign")
    public AjaxResult assignClue(@RequestBody Clue clue){
        try {
            clueService.assignClue(clue);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }

    @PutMapping ("/business")
    public AjaxResult clueChangeBusiness(@RequestBody ClueBusinessDTO clueBusinessDTO){
        try {
            clueService.clueChangeBusiness(clueBusinessDTO);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }


    @GetMapping("Statistical")
    public AjaxResult StatisticalSource(){

        try {
            List<ClueSourceStatisticalVO> list = clueService.StatisticalSource();
            return AjaxResult.me().setResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }

}
